""" 
Copyright 2009 Texas A&M University
 
Licensed under the Apache License, Version 2.0 (the "License"); you may not use 
this file except in compliance with the License. You may obtain a copy of the 
License at

http://www.apache.org/licenses/LICENSE-2.0
  
Unless required by applicable law or agreed to in writing, software distributed 
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
CONDITIONS OF ANY KIND, either express or implied. See the License for the 
specific language governing permissions and limitations under the License.
"""

from zope.component import adapts, queryUtility
from zope.formlib.form import FormFields
from zope.interface import implements
from Products.CMFDefault.formlib.schema import SchemaAdapterBase
from Products.CMFPlone.interfaces import IPloneSiteRoot
from plone.app.controlpanel.form import ControlPanelForm

from meercat.ui.plone.interfaces import IMeercatSchema, _
from meercat.ui.plone.interfaces import IMeercatConnectionConfig



class MeercatControlPanelAdapter(SchemaAdapterBase):
    adapts(IPloneSiteRoot)
    implements(IMeercatSchema)
    
    def _utility(self):
        return queryUtility(IMeercatConnectionConfig)
    
    def getEnabled(self):
        return bool(getattr(self._utility(), 'enabled', False))

    def setEnabled(self, value):
        setattr(self._utility(),'enabled',bool(value))

    enabled = property(getEnabled, setEnabled)
    
    def getSolrEnabled(self):
        return bool(getattr(self._utility(),'solr_enabled',False))
    
    def setSolrEnabled(self,value):
        setattr(self._utility(),'solr_enabled',bool(value))
    
    solr_enabled = property(getSolrEnabled, setSolrEnabled)
    
    def getSolrUrl(self):
        return getattr(self._utility(),'solr_url','')
    
    def setSolrUrl(self,value):
        setattr(self._utility(),'solr_url',value)
    
    solr_url = property(getSolrUrl, setSolrUrl)
    
    def getSolrSslKeyFile(self):
        return getattr(self._utility(),'solr_ssl_key_file','')
    
    def setSolrSslKeyFile(self,value):
        setattr(self._utility(),'solr_ssl_key_file',value)
    
    solr_ssl_key_file = property(getSolrSslKeyFile, setSolrSslKeyFile)
    
    def getSolrSslCertFile(self):
        return getattr(self._utility(),'solr_ssl_cert_file','')
    
    def setSolrSslCertFile(self,value):
        setattr(self._utility(),'solr_ssl_cert_file',value)
    
    solr_ssl_cert_file = property(getSolrSslCertFile, setSolrSslCertFile)
    
    def getSolrTimeout(self):
        return getattr(self._utility(),'solr_timeout','')
    
    def setSolrTimeout(self,value):
        setattr(self._utility(),'solr_timeout',value)
    
    solr_timeout = property(getSolrTimeout, setSolrTimeout)
    
    def getSolrDefaultParameters(self):
        return getattr(self._utility(),'solr_default_parameters',{})
    
    def setSolrDefaultParameters(self,value):
        setattr(self._utility(),'solr_default_parameters',
                dict(map(lambda x: (str(x[0]),x[1]), value.iteritems())))
    
    solr_default_parameters = property(getSolrDefaultParameters, setSolrDefaultParameters)
    
    def getSolrForcedParameters(self):
        return getattr(self._utility(),'solr_forced_parameters',{})
    
    def setSolrForcedParameters(self,value):
        setattr(self._utility(),'solr_forced_parameters',
                dict(map(lambda x: (str(x[0]),x[1]), value.iteritems())))
    
    solr_forced_parameters = property(getSolrForcedParameters, setSolrForcedParameters)
    
    def getRestEnabled(self):
        return bool(getattr(self._utility(),'rest_enabled',False))
    
    def setRestEnabled(self,value):
        setattr(self._utility(),'rest_enabled',bool(value))
    
    rest_enabled = property(getRestEnabled, setRestEnabled)
    
    def getRestUrl(self):
        return getattr(self._utility(),'rest_url','')
    
    def setRestUrl(self,value):
        setattr(self._utility(),'rest_url',value)
    
    rest_url = property(getRestUrl, setRestUrl)
    
    def getRestTimeout(self):
        return getattr(self._utility(),'rest_timeout','')
    
    def setRestTimeout(self,value):
        setattr(self._utility(),'rest_timeout',value)
    
    rest_timeout = property(getRestTimeout, setRestTimeout)
    

class MeercatControlPanel(ControlPanelForm):

    form_fields = FormFields(IMeercatSchema)

    label = _('Meercat settings')
    description = _('Settings to enable and configure Meercat UI for Plone.')
    form_name = _('Meercat settings')

